<%@ page language="java" import="com.erclab.internal.xpresso.security.*" %>

<%@ page errorPage="../xpresso/AppError.jsp" %>

<jsp:useBean id="mySecurity" scope="session" class="com.erclab.internal.xpresso.security.User" />

<%  // -------------------------------------------------------------
    // In this page we'll bind to LDAP to authenticate the user
    // -------------------------------------------------------------

        // Get parameters for the page
        String netId = request.getParameter("userName");
        String password = request.getParameter("password");
        

        if (netId != null && password != null){

            // Create the object to access the ldap methods
            Eldap myLdap = new Eldap();

            // Get the parameters to access LDAP
            ServletContext context = getServletContext();

            myLdap.setInitctx(context.getInitParameter("INITCTX"));
            myLdap.setHost(context.getInitParameter("LDAP_HOST"));
            myLdap.setSearchbase(context.getInitParameter("LDAP_SEARCHBASE"));
            myLdap.setUser(context.getInitParameter("LDAP_USER"));

            // use the method authenticate() to authenticate the user
            boolean authorized  = myLdap.authenticate(netId, password);
            if (authorized) {
                // The bind was succesful with LDAP
                // We can create the the session ID and write the cookie
                // Create an objet to access the user methods

                mySecurity.setNetid(netId);

                if (mySecurity.getIdUser() == null){
                    // Add the user to the database
                    myLdap.addUser(netId);
                    mySecurity.setNetid(netId);
                }

                // ------------------------------------------------------------
                // Add the cookie to avoid the login page when the session ends
                // ------------------------------------------------------------

                    // Get the session ID
                    String sessionId = session.getId();

                    // Update the identifier in the database for this user
                    try {
                        mySecurity.updateUserId(netId, sessionId);
                    } catch (Exception e) {
                        throw new ServletException("Error while logging the user in the application: " + e);
                    }

                    // Add the identifier to a cookie
                    Cookie myCookie = new Cookie("userIdentifier", sessionId);
                    // Set the path of the cookie
                    myCookie.setPath(request.getContextPath());
                    myCookie.setMaxAge(60*60*24*365);
                    response.addCookie(myCookie);

                    // Redirect to the main page:
%>
                <jsp:forward page="../jsp-app/default.jsp" />
<%
            } else {
                // The user was not authorized !!
                String message = "Login failed !";
            }
        } 
%>

<html><!-- InstanceBegin template="/Templates/gedas_template.dwt.asp" codeOutsideHTMLIsLocked="false" -->
<head>
<title>Finance Automated Calculation Tool - gedas USA</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
<link href="../html/css/Styles_gedas.css" rel="stylesheet" type="text/css">
<meta name="Author" content="Enrique Rodriguez">
</head>

<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="740" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top"><table width="740" border="0" cellpadding="0" cellspacing="0">
      <tr valign="bottom">
        <td colspan="2" bgcolor="#003366"><img src="../html/images/Top/Top1.gif" width="544" height="24"></td>
        </tr>
      <tr>
        <td height="6" valign="top" bgcolor="#FF0000"><img src="../html/images/Top/Top2.gif" width="544" height="24"></td>
        <td width="200" rowspan="2"><table width="100%" height="55" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td align="center"><img src="../html/images/Gedas_logo_web.gif" width="140" height="41"></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td height="20" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="37%" rowspan="2"><a href="/jsp-app/default.jsp"><img src="../html/images/Top/HomeDown.gif" width="76" height="29" border="0"></a></td>
              <td width="18%" rowspan="2"><a href="/jsp-app/Reporting.htm"><img src="../html/images/Top/ReportingDown.gif" width="115" height="29" border="0"></a></td>
              <td rowspan="2"><a href="/jsp-app/Administration.htm"><img src="../html/images/Top/AdminDown.gif" width="82" height="29" border="0"></a></td>
              <td width="26%" height="8
			  "><img src="../html/images/Top/ShadowLine.gif" width="271" height="8"></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
        </table></td>
        </tr>
    </table>
      <table width="740"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td align="right">&nbsp;  </td>
        </tr>
      </table>
      <table width="740" border="0" cellspacing="0" cellpadding="0">
        <tr valign="top" background="../html/images/Str_BackgroundTop.gif">
          <td width="19" height="27" align="left" background="../html/images/Str_BackgroundLeft.gif"><img src="../html/images/Str_CornetTopLeft.gif" width="19" height="27"></td>
          <td width="99%" height="27" background="../html/images/Str_BackgroundTop.gif"><!-- InstanceBeginEditable name="PageIcon" -->
								  <img src="../html/images/xpresso/LoginIcon.gif">
							    <!-- InstanceEndEditable --></td>
          <td width="19" height="27" align="right" background="../html/images/Str_BackgroundRight.gif"><img src="../html/images/Str_TopRightCornet.gif" width="19" height="27"></td>
        </tr>
        <tr>
          <td width="19" background="../html/images/Str_BackgroundLeft.gif">&nbsp;          </td>
          <td><table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0" class="IndividualReport">
            <tr>
              <td align="center"><strong> <!-- InstanceBeginEditable name="PageTitle" -->
										Login Page <!-- InstanceEndEditable --> </strong> </td>
            </tr>
            <tr>
              <td align="center"> <!-- InstanceBeginEditable name="PageDescription" -->
										Please provide your LDAP user name and password <!-- InstanceEndEditable --> </td>
            </tr>
            <tr>
              <td>
                <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="140" valign="top">
                      <table width="95%"  border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td width="8"><img src="../html/images/OptionsBox/UpLeftCorner.gif" width="8" height="8"></td>
                          <td background="../html/images/OptionsBox/UpBackground.gif"></td>
                          <td width="7" align="right"><img src="../html/images/OptionsBox/UpRightCorner.gif" width="7" height="8"></td>
                        </tr>
                        <tr>
                          <td width="8" background="../html/images/OptionsBox/RightBackground.gif">&nbsp;</td>
                          <td bgcolor="#FFFFF0"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td class="OptionsBox">Options:</td>
                              </tr>
                              <tr>
                                <td><!-- InstanceBeginEditable name="Options" -->										<!-- InstanceEndEditable --></td>
                              </tr>
                          </table></td>
                          <td width="7" align="right" background="../html/images/OptionsBox/LeftBackground.gif">&nbsp;</td>
                        </tr>
                        <tr>
                          <td width="8"><img src="../html/images/OptionsBox/downLeftCorner.gif" width="8" height="9"></td>
                          <td background="../html/images/OptionsBox/DownBackground.gif"></td>
                          <td width="7" align="right"><img src="../html/images/OptionsBox/DownRigthCorner.gif" width="7" height="9"></td>
                        </tr>
                      </table>
                      </td>
                    <td width="*"><!-- InstanceBeginEditable name="PageContent" -->
								  &nbsp;
                                  <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td><form action="../xpresso/login.jsp" method="post" name="login" id="login">
                                        <p>&nbsp;</p>
                                        <table width="350" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#EFEFEF">
                                          <tr>
                                            <td height="25" align="center" bgcolor="#EFEFEF"><strong>LDAP Authentication </strong></td>
                                          </tr>
                                          <tr>
                                            <td><table width="350" border="0" align="center" cellpadding="2" cellspacing="2" class="loginTable">
                                              <tr>
                                                <td width="145" align="right">User Name :</td>
                                                <td width="13">&nbsp;</td>
                                                <td width="192"><input name="userName" type="text" id="userName" maxlength="15"></td>
                                              </tr>
                                              <tr>
                                                <td align="right">Password :</td>
                                                <td>&nbsp;</td>
                                                <td><input name="password" type="password" id="password2" maxlength="15"></td>
                                              </tr>
                                              <tr>
                                                <td align="right">&nbsp;</td>
                                                <td>&nbsp;</td>
                                                <td><input name="loginButton" type="submit" id="loginButton2" value="Login &gt;&gt;"></td>
                                              </tr>
                                            </table></td>
                                          </tr>
                                        </table>
                                        </form></td>
                                    </tr>
                                  </table>
<!-- InstanceEndEditable --></td>
                  </tr>
              </table></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
          </table></td>
          <td width="19" background="../html/images/Str_BackgroundRight.gif">&nbsp;</td>
        </tr>

        <tr>
          <td colspan="3"><!-- InstanceBeginEditable name="CusomZone" --> <table width="100%"  border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="19" valign="top" background="../html/images/Str_BackgroundLeft.gif">&nbsp;</td>
              <td>&nbsp;</td>
              <td width="19" align="right" valign="top" background="../html/images/Str_BackgroundRight.gif">&nbsp;</td>
            </tr>
          </table><!-- InstanceEndEditable --></td>
        </tr>
        <tr>
          <td width="19" align="left" background="../html/images/Str_BackgroundBottom.gif"><img src="../html/images/Str_BottomLeftCorner.gif" width="19" height="18"></td>
          <td align="right" background="../html/images/Str_BackgroundBottom.gif"><img src="../html/images/Str_gedas.gif" width="183" height="18"></td>
          <td width="19" align="right" background="../html/images/Str_BackgroundBottom.gif"><img src="../html/images/Str_BottomRightCorner.gif" width="19" height="18"></td>
        </tr>
      </table>    </td>
  </tr>
  <tr>
    <td valign="bottom"><br>
      <table width="740" height="25" border="0" cellpadding="0" cellspacing="0" bgcolor="#003063">
        <tr>
          <td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="PageBottom">
            <tr>
              <td width="55" align="center">&nbsp;</td>
              <td align="center">gedas USA 2004 - Finance Automated Calculation Tool <BR>
      Production release: 1.3.4 - <a href="../jsp-app/About.htm"><font color="#FFFFFF">About this release</font></a></td>
              <td width="55" align="center"><a href="../Tutorial/html/index.htm" target="_blank"><img src="../html/images/Button_Help.gif" alt="gFACT Help pages" width="49" height="17" border="0"></a></td>
            </tr>
          </table></td>
        </tr>
      </table></td>
  </tr>
</table>

</body>
<!-- InstanceEnd --></html>
